import cv2
template_pad = {1: (
    [53, 51, 48, 40, 40, 40, 40, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 49, 49, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [21, 12, 11, 10, 8, 7, 6, 6, 5, 4, 4, 5, 7, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
     11,
     11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 2: (
    [51, 23, 18, 16, 16, 15, 14, 14, 14, 14, 14, 24, 26, 25, 26, 26, 26, 26, 26, 26, 25, 24, 22, 13, 13, 13, 13, 14, 15,
     16,
     17, 19, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [33, 8, 5, 3, 2, 2, 1, 1, 1, 2, 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 21, 6, 5, 3, 3, 3, 4, 4, 6, 22, 23,
     23,
     23, 23, 23, 23, 23, 23, 23, 23, 22, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 3: (
    [51, 39, 34, 34, 33, 33, 34, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 25, 25, 23, 20, 11, 1, 1, 1, 1, 3, 3,
     7,
     11, 15, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [34, 8, 5, 4, 3, 2, 2, 2, 3, 2, 21, 22, 23, 23, 23, 24, 24, 24, 24, 25, 24, 24, 8, 9, 10, 11, 11, 11, 10, 9, 20, 22,
     23,
     24, 23, 23, 24, 24, 24, 24, 24, 23, 20, 2, 2, 2, 2, 2, 3, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 4: (
    [48, 43, 37, 35, 30, 28, 23, 21, 17, 15, 15, 15, 21, 23, 27, 29, 33, 35, 41, 41, 40, 39, 3, 1, 1, 1, 1, 1, 1, 1, 1,
     2,
     41, 42, 42, 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [36, 18, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 2,
     1,
     0, 0, 0, 1, 1, 2, 4, 19, 24, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 5: (
    [51, 22, 15, 15, 15, 15, 15, 15, 15, 15, 15, 21, 25, 25, 26, 25, 25, 25, 25, 24, 24, 23, 22, 11, 12, 12, 12, 12, 13,
     14,
     16, 18, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [33, 2, 2, 2, 1, 1, 1, 1, 1, 1, 22, 22, 23, 23, 23, 23, 23, 23, 22, 22, 11, 5, 4, 3, 2, 1, 1, 1, 2, 19, 21, 22, 23,
     23,
     23, 23, 23, 23, 23, 23, 23, 23, 23, 2, 2, 1, 1, 2, 3, 4, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 6: (
    [51, 10, 8, 5, 5, 3, 2, 1, 1, 1, 1, 24, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 25, 23, 12, 12, 12, 13, 14, 15,
     15,
     17, 28, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [35, 8, 7, 5, 5, 4, 3, 3, 3, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 24, 7, 4, 3, 3, 2, 1, 1, 1, 1, 13, 14, 15, 15,
     15,
     15, 15, 15, 15, 15, 15, 15, 15, 13, 1, 1, 2, 2, 4, 6, 10, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 7: (
    [51, 43, 43, 43, 43, 43, 43, 43, 43, 41, 38, 34, 32, 29, 27, 24, 21, 18, 16, 15, 13, 14, 13, 14, 15, 19, 21, 25, 27,
     30,
     32, 37, 40, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [34, 4, 3, 2, 1, 1, 1, 1, 1, 22, 24, 24, 24, 23, 24, 23, 24, 24, 23, 23, 24, 23, 24, 24, 23, 24, 23, 24, 24, 23, 23,
     24,
     23, 24, 24, 23, 23, 23, 24, 24, 23, 23, 23, 23, 24, 24, 23, 23, 23, 24, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 8: (
    [51, 27, 14, 10, 6, 4, 2, 2, 1, 1, 1, 20, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 23, 23, 20, 18, 1, 1, 1, 2, 2, 3,
     6,
     9, 14, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [36, 15, 11, 7, 5, 3, 3, 2, 2, 1, 11, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 2, 3, 5, 6, 9, 9, 7, 5, 3, 3, 14, 14,
     15,
     16, 16, 16, 16, 16, 16, 16, 16, 15, 1, 1, 2, 2, 3, 5, 7, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 9: (
    [51, 28, 26, 18, 14, 13, 12, 11, 11, 12, 18, 25, 25, 25, 25, 26, 26, 26, 25, 25, 25, 26, 25, 24, 23, 3, 1, 1, 3, 3,
     3,
     5, 6, 8, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [35, 14, 9, 7, 5, 3, 1, 1, 1, 1, 12, 13, 16, 15, 15, 15, 16, 16, 16, 16, 16, 15, 15, 15, 1, 1, 1, 1, 1, 2, 3, 4, 25,
     25,
     25, 25, 26, 25, 25, 25, 26, 25, 14, 4, 3, 3, 4, 4, 5, 7, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])}
template_pass = {1: (
    [23, 18, 17, 1, 0, 0, 0, 0, 22, 22, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [7, 5, 4, 3, 3, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 2: (
    [24, 13, 8, 8, 7, 7, 12, 12, 12, 12, 12, 11, 6, 6, 7, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [17, 5, 2, 1, 2, 11, 12, 12, 12, 12, 12, 4, 2, 3, 3, 12, 12, 12, 12, 12, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 3: (
    [18, 17, 17, 13, 13, 13, 13, 13, 13, 13, 13, 12, 6, 1, 1, 1, 3, 7, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [19, 3, 2, 1, 1, 1, 12, 12, 13, 13, 14, 13, 5, 6, 5, 4, 12, 13, 12, 13, 13, 12, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 4: (
    [24, 21, 17, 14, 11, 9, 8, 11, 14, 16, 18, 20, 20, 2, 1, 1, 1, 1, 2, 21, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [21, 10, 9, 9, 9, 10, 9, 9, 10, 10, 9, 10, 10, 9, 1, 0, 1, 2, 13, 15, 15, 15, 15, 15, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 5: (
    [23, 8, 7, 7, 7, 7, 11, 11, 11, 11, 11, 10, 5, 5, 6, 7, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [17, 1, 1, 1, 2, 12, 12, 12, 12, 3, 2, 1, 1, 11, 12, 12, 12, 12, 12, 2, 1, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 6: (
    [25, 6, 3, 3, 1, 1, 1, 13, 13, 13, 13, 13, 13, 12, 12, 6, 6, 7, 8, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [20, 5, 3, 3, 2, 14, 14, 14, 14, 14, 3, 2, 1, 1, 7, 9, 9, 9, 9, 9, 9, 1, 1, 3, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 7: (
    [21, 20, 20, 20, 19, 16, 14, 11, 9, 7, 7, 7, 9, 11, 14, 16, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [17, 1, 0, 0, 1, 11, 11, 12, 11, 12, 11, 12, 12, 11, 12, 11, 12, 12, 12, 12, 11, 12, 11, 13, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 8: (
    [27, 14, 6, 3, 2, 1, 1, 11, 12, 12, 12, 12, 12, 12, 12, 11, 1, 1, 1, 2, 4, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [22, 8, 4, 2, 2, 1, 9, 10, 10, 10, 10, 3, 2, 4, 6, 3, 2, 9, 10, 10, 10, 10, 9, 1, 2, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 9: (
    [26, 16, 12, 8, 6, 6, 9, 12, 13, 14, 14, 13, 13, 13, 11, 1, 1, 3, 3, 4, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [21, 8, 5, 3, 1, 6, 9, 9, 9, 10, 10, 9, 1, 1, 2, 2, 14, 15, 15, 15, 15, 8, 3, 3, 4, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])}
class image:
    def __init__(self, img, (x, y), (w, h)):
        self.img = img
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.result = 0
        self.sum_x = [0] * 100
        self.sum_y = [0] * 100
        self.sum()

    def settemplate(self, template):
        self.template = template

    def sum(self):
        self.sum_x = [0] * 100
        self.sum_y = [0] * 100
        for i in range(self.h):
            for j in range(self.w):
                if self.img[i][j][0] == 0:
                    self.sum_x[j] += 1
                    self.sum_y[i] += 1
        return (self.sum_x, self.sum_y)

    def recognize(self):
        temp_dict = {}
        for i in range(1, 10):
            error = 0
            template_x, template_y = self.template[i]
            for j in range(100):
                error += abs(template_x[j] - self.sum_x[j]) + abs(template_y[j] - self.sum_y[j])
            temp_dict[i] = error
        value_min = min(temp_dict.itervalues())
        self.result = [k for k, v in temp_dict.iteritems() if v == value_min][0]
image_pass = []
image_pad = []

raw_image = cv2.imread("/Users/wzq/Downloads/1/jgg.bmp")

edge = cv2.Canny(raw_image, 500, 2000, apertureSize=5)
contours, hierarchy = cv2.findContours(edge.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

x_last = None
y_last = None
for i in contours:
    x, y, w, h = cv2.boundingRect(i)
    if x_last == x and y_last == y:
        x_last = x
        y_last = y
        continue
    x_last = x
    y_last = y
    if w > 40 or h > 60 or h < 20:  # give up useless part
        continue
    if y > 86:
        temp = image(raw_image[y:y + h, x:x + w].copy(), (x, y), (w, h))
        temp.settemplate(template_pad)
        temp.recognize()
        image_pad.append(temp)
        cv2.rectangle(raw_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    else:
        temp = image(raw_image[y:y + h, x:x + w].copy(), (x, y), (w, h))
        temp.settemplate(template_pass)
        temp.recognize()
        image_pass.append(temp)
        cv2.rectangle(raw_image, (x, y), (x + w, y + h), (0, 0, 255), 2)
image_pad.sort(cmp=lambda x, y: 1 if abs(x.y - y.y) < 5 and x.x > y.x else -1)
image_pass.sort(cmp=lambda x, y: 1 if x.x > y.x else -1)

print "password:", map(lambda x: x.result, image_pass)
print "pad:", map(lambda x: x.result, image_pad)
print "reflect:", map(lambda x: [i for i,y in enumerate(image_pad) if y.result==x.result][0]+1, image_pass)
cv2.imshow("digit recognition", raw_image)
cv2.waitKey(0)
